using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace AjGenesisStudio.Dialogs
{
    public partial class NewFileDialog : Form
    {
        public NewFileDialog()
        {
            InitializeComponent();
        }

        private void NewFileDialog_Load(object sender, EventArgs e)
        {
            FillList();
        }

        private void FillList()
        {
            string dir_path;
            ListViewItem item;
            ListViewGroup group;

            dir_path = AppDomain.CurrentDomain.BaseDirectory + "ItemTemplates";
            if (!Directory.Exists(dir_path))
            {
                MessageBox.Show("Can not find the 'ItemTemplates' directory.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            foreach (string folder in Directory.GetDirectories(dir_path))
            {
                group = new ListViewGroup();
                group.Header = (new DirectoryInfo(folder)).Name;
                lstFiles.Groups.Add(group);

                foreach (string file in Directory.GetFiles(folder))
                {
                    item = new ListViewItem();
                    item.Text = Path.GetFileNameWithoutExtension(file);
                    item.Tag = file;
                    item.ImageIndex = Utils.IconExtraction.GetIcon(imagesLarge, Path.GetExtension(file), true);
                    Utils.IconExtraction.GetIcon(imagesSmall, Path.GetExtension(file), false);
                    item.Group = group;
                    lstFiles.Items.Add(item);
                }
            }

            foreach (string file in Directory.GetFiles(dir_path))
            {
                item = new ListViewItem();
                item.Text = Path.GetFileNameWithoutExtension(file);
                item.Tag = file;
                item.ImageIndex = Utils.IconExtraction.GetIcon(imagesLarge, Path.GetExtension(file), true);
                Utils.IconExtraction.GetIcon(imagesSmall, Path.GetExtension(file), false);
                lstFiles.Items.Add(item);
            }
        }

        private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnOpen.Enabled = (lstFiles.SelectedItems.Count > 0);
        }

        private void btnLarge_CheckedChanged(object sender, EventArgs e)
        {
            if (btnLarge.Checked)
                lstFiles.View = View.LargeIcon;
            else
                lstFiles.View = View.SmallIcon;
        }

        private void btnSmall_CheckedChanged(object sender, EventArgs e)
        {
            if (btnSmall.Checked)
                lstFiles.View = View.SmallIcon;
            else
                lstFiles.View = View.LargeIcon;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            Commands.NewFileFromTemplateCommand cmd;
            cmd = new Commands.NewFileFromTemplateCommand();
            cmd.TemplatePath = lstFiles.SelectedItems[0].Tag.ToString();
            cmd.Execute();
        }
    }
}